package com.df.springmvc.system.manage.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import oracle.net.aso.e;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.common.bean.SQLResultBean;
import com.common.bean.SQLUpdateBean;
import com.common.util.security.EncrypMD5;
import com.df.springmvc.common.bean.CurrentUserBean;
import com.df.springmvc.supper.SupperDao;
import com.df.springmvc.system.log.services.LogServices;

/**
 * 本类只做数据库操作处理
 * @author David
 */
@Repository
public class LoginDao extends SupperDao{
	
	@Autowired
	LogServices logServices;
	/**
	 * 登录时查询用户信息
	 * @param u 用户账号
	 * @param p 密码
	 * @return
	 * @throws Exception
	 */
	public ArrayList<String> valiUser2(String u, String p) throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		String sql = "SELECT USERACCOUNT as USERNAME,USERACCOUNT from S_USER_INFO  WHERE STATUS='1' AND USERACCOUNT= '" + u + "' AND PASSWORD= '" + p + "' ";
		Connection connection = dataSource2.getConnection();
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement.executeQuery(sql);
		ResultSetMetaData metaData = resultSet.getMetaData();
		int count = metaData.getColumnCount();
		while(resultSet.next()){
			for(int i = 1 ; i <= count ; i++ ){
				list.add(i-1,resultSet.getString(i));
			}
		}
		connection.close();
		if(list.size()>0){
			return list;
		}else{
			throw new Exception("用户名或密码错误");
		}
	}
	/**
	 * 密码修改
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public Object retrievePassword(HashMap<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("userAccount",map.get("userAccount"));
		Object rs=excuteSqlBean(new SQLUpdateBean("S_USER_INFO",map,tmp){});
		return rs;
	}
	/**
	 * 通过手机号码和账号对比该用户是否存在
	 * @param map
	 * @return
	 */
	public boolean queryVerify(HashMap<String, String> map) {
		// TODO Auto-generated method stub
		Session session2=getSessionFactory().getCurrentSession();
		String sql2="select 0 from s_users_info o  left join b_staff_info s on s.userAccount=o.userAccount  where o.userType='1'  and o.STATUS = '1' and o.accountStatus='1' and s.status='1' and s.incumbencyStatus='1'  and s.telphone=:telphone  and o.userAccount=:userAccount or s.staffNumCode=:staffNumCode ";
		SQLQuery query2=session2.createSQLQuery(sql2);
		query2.setString("telphone",map.get("telphone"));
		query2.setString("userAccount",map.get("userAccount"));
		query2.setString("staffNumCode",map.get("userAccount"));
		if(query2.list().size() > 0){
			return true;
		}else{
			return false;
		}
	}
}
